﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace CRMAdapter
{
    public static class PayByEzidebit
    {
        //Add new customer to EziDebit
        public static XmlDocument AddCustomers(string DigitalKey, string YourSystemReference, string YourGeneralReference, string LastName, string FirstName, string AddressLine1, string AddressLine2, string AddressSuburb, string AddressState, string AddressPostCode, string EmailAddress, string MobilePhoneNumber, string ContractStartDate, string SmsPaymentReminder, string SmsFailedNotification, string SmsExpiredCard, string Username)
        {
            XmlDocument doc;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfNewCustomerXcXH3liW rs = cs.AddCustomer(DigitalKey, YourSystemReference, YourGeneralReference, LastName, FirstName, AddressLine1, AddressLine2, AddressSuburb, AddressState, AddressPostCode, EmailAddress, MobilePhoneNumber, ContractStartDate, SmsPaymentReminder, SmsFailedNotification, SmsExpiredCard, Username);

                doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode dataNode = doc.CreateElement("Data");
                doc.AppendChild(dataNode);

                if (rs.Data == null && rs.Error > 0)
                {
                    XmlNode errorNode = doc.CreateElement("Error");
                    errorNode.AppendChild(doc.CreateTextNode(rs.Error.ToString()));
                    dataNode.AppendChild(errorNode);

                    XmlNode errorMessageNode = doc.CreateElement("ErrorMessage");
                    errorMessageNode.AppendChild(doc.CreateTextNode(rs.ErrorMessage));
                    dataNode.AppendChild(errorMessageNode);
                }
                else
                {
                    XmlNode customerRefNode = doc.CreateElement("a:CustomerRef");
                    customerRefNode.AppendChild(doc.CreateTextNode(rs.Data.CustomerRef.ToString()));
                    dataNode.AppendChild(customerRefNode);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }

        //Edit customer to EziDebit
        public static string EditCustomers(string DigitalKey, string YourSystemReference, string YourGeneralReference, string LastName, string FirstName, string AddressLine1, string AddressLine2, string AddressSuburb, string AddressState, string AddressPostCode, string EmailAddress, string MobilePhoneNumber, string SmsPaymentReminder, string SmsFailedNotification, string SmsExpiredCard, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.EditCustomerDetails(DigitalKey, "", YourSystemReference, "", YourGeneralReference, LastName, FirstName, AddressLine1, AddressLine2, AddressSuburb, AddressState, AddressPostCode, EmailAddress, MobilePhoneNumber, SmsPaymentReminder, SmsFailedNotification, SmsExpiredCard, Username);

                if (rs.Data == "S")
                    message = "Customer Updated Successfully";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Edit customer to EziDebit
        public static string CreateSchedule(string DigitalKey, string YourSystemReference, string ScheduleStartDate, string SchedulePeriodType, string DayOfWeek, int DayOfMonth, string FirstWeekOfMonth, string SecondWeekOfMonth, string ThirdWeekOfMonth, string FourthWeekOfMonth, long PaymentAmountInCents, int LimitToNumberOfPayments, long LimitToTotalAmountInCents, string KeepManualPayments, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.CreateSchedule(DigitalKey, "", YourSystemReference, ScheduleStartDate, SchedulePeriodType, DayOfWeek, DayOfMonth, FirstWeekOfMonth, SecondWeekOfMonth, ThirdWeekOfMonth, FourthWeekOfMonth, PaymentAmountInCents, LimitToNumberOfPayments, LimitToTotalAmountInCents, KeepManualPayments, Username);

                if (rs.Data == "S")
                    message = "Schedule Created Successfully";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Add new customer to EziDebit from contract page
        public static int AddCustomersFromContractPage(string DigitalKey, string YourSystemReference, string YourGeneralReference, string LastName, string FirstName, string AddressLine1, string AddressLine2, string AddressSuburb, string AddressState, string AddressPostCode, string EmailAddress, string MobilePhoneNumber, string ContractStartDate, string SmsPaymentReminder, string SmsFailedNotification, string SmsExpiredCard, string Username)
        {
            EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
            EziDebitService.EziResponseOfNewCustomerXcXH3liW rs;
            try
            {
                rs = cs.AddCustomer(DigitalKey, YourSystemReference, YourGeneralReference, LastName, FirstName, AddressLine1, AddressLine2, AddressSuburb, AddressState, AddressPostCode, EmailAddress, MobilePhoneNumber, ContractStartDate, SmsPaymentReminder, SmsFailedNotification, SmsExpiredCard, Username);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs.Data.CustomerRef;
        }

        //Add payment plan to EziDebit
        public static string AddPayments(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, string DebitDate, long PaymentAmountInCents, string PaymentReference, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.AddPayment(DigitalKey, EziDebitCustomerID, YourSystemReference, DebitDate, PaymentAmountInCents, PaymentReference, Username);
                if (rs.Data == "S")
                    message = "Payment Added Successfully";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Add payment plan to EziDebit form contract
        public static void AddPaymentsFromContract(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, string DebitDate, long PaymentAmountInCents, string PaymentReference, string Username)
        {
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs = cs.AddPayment(DigitalKey, EziDebitCustomerID, YourSystemReference, DebitDate, PaymentAmountInCents, PaymentReference, Username);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Get payment result:
        public static XmlDocument GetPaymentDetails(string DigitalKey, string PaymentReference)
        {
            XmlDocument doc;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfPaymentDetailTHgMB7oL payDetails = cs.GetPaymentDetail(DigitalKey, PaymentReference);
              
                doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode dataNode = doc.CreateElement("Data");
                doc.AppendChild(dataNode);

                if (payDetails.Data == null && payDetails.Error > 0)
                {
                    XmlNode errorNode = doc.CreateElement("Error");
                    errorNode.AppendChild(doc.CreateTextNode(payDetails.Error.ToString()));
                    dataNode.AppendChild(errorNode);

                    XmlNode errorMessageNode = doc.CreateElement("ErrorMessage");
                    errorMessageNode.AppendChild(doc.CreateTextNode(payDetails.ErrorMessage));
                    dataNode.AppendChild(errorMessageNode);
                }
                else
                {
                    XmlNode bankFailedReasonNode = doc.CreateElement("BankFailedReason");
                    bankFailedReasonNode.AppendChild(doc.CreateTextNode(payDetails.Data.BankFailedReason));
                    dataNode.AppendChild(bankFailedReasonNode);

                    XmlNode bankReturnCodeNode = doc.CreateElement("BankReturnCode");
                    bankReturnCodeNode.AppendChild(doc.CreateTextNode(payDetails.Data.BankReturnCode));
                    dataNode.AppendChild(bankReturnCodeNode);

                    XmlNode debitDateNode = doc.CreateElement("DebitDate");
                    debitDateNode.AppendChild(doc.CreateTextNode(payDetails.Data.DebitDate.ToString()));
                    dataNode.AppendChild(debitDateNode);

                    XmlNode invoiceIDNode = doc.CreateElement("InvoiceID");
                    invoiceIDNode.AppendChild(doc.CreateTextNode(payDetails.Data.InvoiceID));
                    dataNode.AppendChild(invoiceIDNode);

                    XmlNode paymentAmountNode = doc.CreateElement("PaymentAmount");
                    paymentAmountNode.AppendChild(doc.CreateTextNode(payDetails.Data.PaymentAmount.ToString()));
                    dataNode.AppendChild(paymentAmountNode);

                    XmlNode paymentIDNode = doc.CreateElement("PaymentID");
                    paymentIDNode.AppendChild(doc.CreateTextNode(payDetails.Data.PaymentID));
                    dataNode.AppendChild(paymentIDNode);

                    XmlNode paymentMethodNode = doc.CreateElement("PaymentMethod");
                    paymentMethodNode.AppendChild(doc.CreateTextNode(payDetails.Data.PaymentMethod));
                    dataNode.AppendChild(paymentMethodNode);

                    XmlNode paymentReferenceNode = doc.CreateElement("PaymentReference");
                    paymentReferenceNode.AppendChild(doc.CreateTextNode(payDetails.Data.PaymentReference));
                    dataNode.AppendChild(paymentReferenceNode);

                    XmlNode paymentStatusNode = doc.CreateElement("PaymentStatus");
                    paymentStatusNode.AppendChild(doc.CreateTextNode(payDetails.Data.PaymentStatus));
                    dataNode.AppendChild(paymentAmountNode);

                    XmlNode settlementDateNode = doc.CreateElement("SettlementDate");
                    settlementDateNode.AppendChild(doc.CreateTextNode(payDetails.Data.SettlementDate.ToString()));
                    dataNode.AppendChild(settlementDateNode);

                    XmlNode scheduledAmountNode = doc.CreateElement("ScheduledAmount");
                    scheduledAmountNode.AppendChild(doc.CreateTextNode(payDetails.Data.ScheduledAmount.ToString()));
                    dataNode.AppendChild(scheduledAmountNode);

                    XmlNode transactionFeeClientNode = doc.CreateElement("TransactionFeeClient");
                    transactionFeeClientNode.AppendChild(doc.CreateTextNode(payDetails.Data.TransactionFeeClient.ToString()));
                    dataNode.AppendChild(transactionFeeClientNode);

                    XmlNode transactionFeeCustomerNode = doc.CreateElement("TransactionFeeCustomer");
                    transactionFeeCustomerNode.AppendChild(doc.CreateTextNode(payDetails.Data.TransactionFeeCustomer.ToString()));
                    dataNode.AppendChild(transactionFeeCustomerNode);

                    XmlNode yourGeneralReferenceNode = doc.CreateElement("YourGeneralReference");
                    yourGeneralReferenceNode.AppendChild(doc.CreateTextNode(payDetails.Data.YourGeneralReference));
                    dataNode.AppendChild(yourGeneralReferenceNode);

                    XmlNode yourSystemReferenceNode = doc.CreateElement("YourSystemReference");
                    yourSystemReferenceNode.AppendChild(doc.CreateTextNode(payDetails.Data.YourSystemReference));
                    dataNode.AppendChild(yourSystemReferenceNode);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }

        //Change schedule amount
        public static string ChangeScheduledAmounts(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, int ChangeFromPaymentNumber, string ChangeFromDate, long NewPaymentAmountInCents, string ApplyToAllFuturePayments, string KeepManualPayments, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring res = cs.ChangeScheduledAmount(DigitalKey, EziDebitCustomerID, YourSystemReference, ChangeFromPaymentNumber, ChangeFromDate, NewPaymentAmountInCents, ApplyToAllFuturePayments, KeepManualPayments, Username);
                 if (res.Data == "S")
                    message = "Amount has been changed successfully";
                else
                    message = res.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Change schedule date
        public static string ChangeScheduledDates(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, string ChangeFromDate, string PaymentReference, string ChangeToDate, string KeepManualPayments, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring res = cs.ChangeScheduledDate(DigitalKey, EziDebitCustomerID, YourSystemReference, ChangeFromDate, PaymentReference, ChangeToDate, KeepManualPayments, Username);
                if (res.Data == "S")
                    message = "Date has been changed successfully";
                else
                    message = res.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Get Scheduled Payments
        public static XmlDocument GetScheduledPayments(string DigitalKey, string DateFrom, string DateTo, string EziDebitCustomerID, string YourSystemReference)
        {
            XmlDocument doc;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfArrayOfScheduledPaymentTHgMB7oL res;
                res = cs.GetScheduledPayments(DigitalKey, DateFrom, DateTo, EziDebitCustomerID, YourSystemReference);

                doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode dataNode = doc.CreateElement("Data");
                doc.AppendChild(dataNode);

                if (res.Data == null && res.Error > 0)
                {
                    XmlNode errorNode = doc.CreateElement("Error");
                    errorNode.AppendChild(doc.CreateTextNode(res.Error.ToString()));
                    dataNode.AppendChild(errorNode);

                    XmlNode errorMessageNode = doc.CreateElement("ErrorMessage");
                    errorMessageNode.AppendChild(doc.CreateTextNode(res.ErrorMessage));
                    dataNode.AppendChild(errorMessageNode);
                }
                else
                {
                    for (int i = 0; i < Int32.Parse(res.Data.Count().ToString()); i++)
                    {
                        XmlNode scheduledPaymentNode = doc.CreateElement("ScheduledPayment");
                        dataNode.AppendChild(scheduledPaymentNode);

                        XmlNode ezidebitCustomerIDNode = doc.CreateElement("EzidebitCustomerID");
                        ezidebitCustomerIDNode.AppendChild(doc.CreateTextNode(res.Data[i].EzidebitCustomerID));
                        scheduledPaymentNode.AppendChild(ezidebitCustomerIDNode);

                        XmlNode manuallyAddedPaymentNode = doc.CreateElement("ManuallyAddedPayment");
                        manuallyAddedPaymentNode.AppendChild(doc.CreateTextNode(res.Data[i].ManuallyAddedPayment.ToString()));
                        scheduledPaymentNode.AppendChild(manuallyAddedPaymentNode);

                        XmlNode paymentAmountNode = doc.CreateElement("PaymentAmount");
                        paymentAmountNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentAmount.ToString()));
                        scheduledPaymentNode.AppendChild(paymentAmountNode);

                        XmlNode paymentDateNode = doc.CreateElement("PaymentDate");
                        paymentDateNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentDate.ToString()));
                        scheduledPaymentNode.AppendChild(paymentDateNode);

                        XmlNode paymentReferenceNode = doc.CreateElement("PaymentReference");
                        paymentReferenceNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentReference));
                        scheduledPaymentNode.AppendChild(paymentReferenceNode);

                        XmlNode yourGeneralReferenceNode = doc.CreateElement("YourGeneralReference");
                        yourGeneralReferenceNode.AppendChild(doc.CreateTextNode(res.Data[i].YourGeneralReference));
                        scheduledPaymentNode.AppendChild(yourGeneralReferenceNode);

                        XmlNode yourSystemReferenceNode = doc.CreateElement("YourSystemReference");
                        yourSystemReferenceNode.AppendChild(doc.CreateTextNode(res.Data[i].YourSystemReference));
                        scheduledPaymentNode.AppendChild(yourSystemReferenceNode);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }

        //Get Payments
        public static XmlDocument GetPayments(string DigitalKey, string PaymentType, string PaymentMethod, string PaymentSource, string PaymentReference, string DateFrom, string DateTo, string DateField, string EzidebitCustomerID, string YourSystemReference)
        {
            XmlDocument doc;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfArrayOfPaymentTHgMB7oL res = cs.GetPayments(DigitalKey, PaymentType, PaymentMethod, PaymentSource, "", "", "", "", "", "");

                doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode dataNode = doc.CreateElement("Data");
                doc.AppendChild(dataNode);

                if (res.Data == null && res.Error > 0)
                {
                    XmlNode errorNode = doc.CreateElement("Error");
                    errorNode.AppendChild(doc.CreateTextNode(res.Error.ToString()));
                    dataNode.AppendChild(errorNode);

                    XmlNode errorMessageNode = doc.CreateElement("ErrorMessage");
                    errorMessageNode.AppendChild(doc.CreateTextNode(res.ErrorMessage));
                    dataNode.AppendChild(errorMessageNode);
                }
                else
                {
                    for (int i = 0; i < Int32.Parse(res.Data.Count().ToString()); i++)
                    {
                        XmlNode paymentNode = doc.CreateElement("Payment");
                        dataNode.AppendChild(paymentNode);

                        XmlNode bankFailedReasonNode = doc.CreateElement("BankFailedReason");
                        bankFailedReasonNode.AppendChild(doc.CreateTextNode(res.Data[i].BankFailedReason));
                        paymentNode.AppendChild(bankFailedReasonNode);

                        XmlNode bankReceiptIDNode = doc.CreateElement("BankReceiptID");
                        bankReceiptIDNode.AppendChild(doc.CreateTextNode(res.Data[i].BankReceiptID));
                        paymentNode.AppendChild(bankReceiptIDNode);

                        XmlNode bankReturnCodeNode = doc.CreateElement("BankReturnCode");
                        bankReturnCodeNode.AppendChild(doc.CreateTextNode(res.Data[i].BankReturnCode));
                        paymentNode.AppendChild(bankReturnCodeNode);

                        XmlNode customerNameNode = doc.CreateElement("CustomerName");
                        customerNameNode.AppendChild(doc.CreateTextNode(res.Data[i].CustomerName));
                        paymentNode.AppendChild(customerNameNode);

                        XmlNode debitDateNode = doc.CreateElement("DebitDate");
                        debitDateNode.AppendChild(doc.CreateTextNode(res.Data[i].DebitDate.ToString()));
                        paymentNode.AppendChild(debitDateNode);

                        XmlNode ezidebitCustomerIDNode = doc.CreateElement("EzidebitCustomerID");
                        ezidebitCustomerIDNode.AppendChild(doc.CreateTextNode(res.Data[i].EzidebitCustomerID));
                        paymentNode.AppendChild(ezidebitCustomerIDNode);

                        XmlNode invoiceIDNode = doc.CreateElement("InvoiceID");
                        invoiceIDNode.AppendChild(doc.CreateTextNode(res.Data[i].InvoiceID));
                        paymentNode.AppendChild(invoiceIDNode);

                        XmlNode paymentAmountNode = doc.CreateElement("PaymentAmount");
                        paymentAmountNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentAmount.ToString()));
                        paymentNode.AppendChild(paymentAmountNode);

                        XmlNode paymentIDNode = doc.CreateElement("PaymentID");
                        paymentIDNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentID));
                        paymentNode.AppendChild(paymentIDNode);

                        XmlNode paymentMethodNode = doc.CreateElement("PaymentMethod");
                        paymentMethodNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentMethod));
                        paymentNode.AppendChild(paymentMethodNode);

                        XmlNode paymentReferenceNode = doc.CreateElement("PaymentReference");
                        paymentReferenceNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentReference));
                        paymentNode.AppendChild(paymentReferenceNode);

                        XmlNode paymentSourceNode = doc.CreateElement("PaymentSource");
                        paymentSourceNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentSource));
                        paymentNode.AppendChild(paymentSourceNode);

                        XmlNode paymentStatusNode = doc.CreateElement("PaymentStatus");
                        paymentStatusNode.AppendChild(doc.CreateTextNode(res.Data[i].PaymentStatus));
                        paymentNode.AppendChild(paymentStatusNode);

                        XmlNode settlementDateNode = doc.CreateElement("SettlementDate");
                        settlementDateNode.AppendChild(doc.CreateTextNode(res.Data[i].SettlementDate.ToString()));
                        paymentNode.AppendChild(settlementDateNode);

                        XmlNode scheduledAmountNode = doc.CreateElement("ScheduledAmount");
                        scheduledAmountNode.AppendChild(doc.CreateTextNode(res.Data[i].ScheduledAmount.ToString()));
                        paymentNode.AppendChild(scheduledAmountNode);

                        XmlNode transactionFeeClientNode = doc.CreateElement("TransactionFeeClient");
                        transactionFeeClientNode.AppendChild(doc.CreateTextNode(res.Data[i].TransactionFeeClient.ToString()));
                        paymentNode.AppendChild(transactionFeeClientNode);

                        XmlNode transactionFeeCustomerNode = doc.CreateElement("TransactionFeeCustomer");
                        transactionFeeCustomerNode.AppendChild(doc.CreateTextNode(res.Data[i].TransactionFeeCustomer.ToString()));
                        paymentNode.AppendChild(transactionFeeCustomerNode);

                        XmlNode transactionTimeNode = doc.CreateElement("TransactionTime");
                        transactionTimeNode.AppendChild(doc.CreateTextNode(res.Data[i].TransactionTime.ToString()));
                        paymentNode.AppendChild(transactionTimeNode);

                        XmlNode yourGeneralReferenceNode = doc.CreateElement("YourGeneralReference");
                        yourGeneralReferenceNode.AppendChild(doc.CreateTextNode(res.Data[i].YourGeneralReference));
                        paymentNode.AppendChild(yourGeneralReferenceNode);

                        XmlNode yourSystemReferenceNode = doc.CreateElement("YourSystemReference");
                        yourSystemReferenceNode.AppendChild(doc.CreateTextNode(res.Data[i].YourSystemReference));
                        paymentNode.AppendChild(yourSystemReferenceNode);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }

        //Get Customer Details
        public static XmlDocument GetCustomerDetails(string DigitalKey, string EziDebitCustomerID, string YourSystemReference)
        {
            XmlDocument doc;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfCustomerDetailsTHgMB7oL res;
                res = cs.GetCustomerDetails(DigitalKey, EziDebitCustomerID, YourSystemReference);

                doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode dataNode = doc.CreateElement("Data");
                doc.AppendChild(dataNode);

                if (res.Data == null && res.Error > 0)
                {
                    XmlNode errorNode = doc.CreateElement("Error");
                    errorNode.AppendChild(doc.CreateTextNode(res.Error.ToString()));
                    dataNode.AppendChild(errorNode);

                    XmlNode errorMessageNode = doc.CreateElement("ErrorMessage");
                    errorMessageNode.AppendChild(doc.CreateTextNode(res.ErrorMessage));
                    dataNode.AppendChild(errorMessageNode);
                }
                else
                {
                    XmlNode addressLine1Node = doc.CreateElement("AddressLine1");
                    addressLine1Node.AppendChild(doc.CreateTextNode(res.Data.AddressLine1));
                    dataNode.AppendChild(addressLine1Node);

                    XmlNode AddressLine2Node = doc.CreateElement("AddressLine2");
                    AddressLine2Node.AppendChild(doc.CreateTextNode(res.Data.AddressLine2));
                    dataNode.AppendChild(AddressLine2Node);

                    XmlNode addressPostCodeNode = doc.CreateElement("AddressPostCode");
                    addressPostCodeNode.AppendChild(doc.CreateTextNode(res.Data.AddressPostCode));
                    dataNode.AppendChild(addressPostCodeNode);

                    XmlNode addressStateNode = doc.CreateElement("AddressState");
                    addressStateNode.AppendChild(doc.CreateTextNode(res.Data.AddressState));
                    dataNode.AppendChild(addressStateNode);

                    XmlNode addressSuburbNode = doc.CreateElement("AddressSuburb");
                    addressSuburbNode.AppendChild(doc.CreateTextNode(res.Data.AddressSuburb));
                    dataNode.AppendChild(addressSuburbNode);

                    XmlNode contractStartDateNode = doc.CreateElement("ContractStartDate");
                    contractStartDateNode.AppendChild(doc.CreateTextNode(res.Data.ContractStartDate.ToString()));
                    dataNode.AppendChild(contractStartDateNode);

                    XmlNode customerFirstNameNode = doc.CreateElement("CustomerFirstName");
                    customerFirstNameNode.AppendChild(doc.CreateTextNode(res.Data.CustomerFirstName));
                    dataNode.AppendChild(customerFirstNameNode);

                    XmlNode customerNameNode = doc.CreateElement("CustomerName");
                    customerNameNode.AppendChild(doc.CreateTextNode(res.Data.CustomerName));
                    dataNode.AppendChild(customerNameNode);


                    XmlNode emailNode = doc.CreateElement("Email");
                    emailNode.AppendChild(doc.CreateTextNode(res.Data.Email));
                    dataNode.AppendChild(emailNode);

                    XmlNode ezidebitCustomerIDNode = doc.CreateElement("EzidebitCustomerID");
                    ezidebitCustomerIDNode.AppendChild(doc.CreateTextNode(res.Data.EzidebitCustomerID));
                    dataNode.AppendChild(ezidebitCustomerIDNode);

                    XmlNode mobilePhoneNode = doc.CreateElement("MobilePhone");
                    mobilePhoneNode.AppendChild(doc.CreateTextNode(res.Data.MobilePhone));
                    dataNode.AppendChild(mobilePhoneNode);

                    XmlNode paymentMethodNode = doc.CreateElement("PaymentMethod");
                    paymentMethodNode.AppendChild(doc.CreateTextNode(res.Data.PaymentMethod));
                    dataNode.AppendChild(paymentMethodNode);

                    XmlNode paymentPeriodNode = doc.CreateElement("PaymentPeriod");
                    paymentPeriodNode.AppendChild(doc.CreateTextNode(res.Data.PaymentPeriod));
                    dataNode.AppendChild(paymentPeriodNode);

                    XmlNode paymentPeriodDayOfMonthNode = doc.CreateElement("PaymentPeriodDayOfMonth");
                    paymentPeriodDayOfMonthNode.AppendChild(doc.CreateTextNode(res.Data.PaymentPeriodDayOfMonth));
                    dataNode.AppendChild(paymentPeriodDayOfMonthNode);

                    XmlNode paymentPeriodDayOfWeekNode = doc.CreateElement("PaymentPeriodDayOfWeek");
                    paymentPeriodDayOfWeekNode.AppendChild(doc.CreateTextNode(res.Data.PaymentPeriodDayOfWeek));
                    dataNode.AppendChild(paymentPeriodDayOfWeekNode);

                    XmlNode smsExpiredCardNode = doc.CreateElement("SmsExpiredCard");
                    smsExpiredCardNode.AppendChild(doc.CreateTextNode(res.Data.SmsExpiredCard));
                    dataNode.AppendChild(smsExpiredCardNode);

                    XmlNode smsFailedNotificationNode = doc.CreateElement("SmsFailedNotification");
                    smsFailedNotificationNode.AppendChild(doc.CreateTextNode(res.Data.SmsFailedNotification));
                    dataNode.AppendChild(smsFailedNotificationNode);

                    XmlNode smsPaymentReminderNode = doc.CreateElement("SmsPaymentReminder");
                    smsPaymentReminderNode.AppendChild(doc.CreateTextNode(res.Data.SmsPaymentReminder));
                    dataNode.AppendChild(smsPaymentReminderNode);

                    XmlNode statusCodeNode = doc.CreateElement("StatusCode");
                    statusCodeNode.AppendChild(doc.CreateTextNode(res.Data.StatusCode));
                    dataNode.AppendChild(statusCodeNode);

                    XmlNode statusDescriptionNode = doc.CreateElement("StatusDescription");
                    statusDescriptionNode.AppendChild(doc.CreateTextNode(res.Data.StatusDescription));
                    dataNode.AppendChild(statusDescriptionNode);

                    XmlNode totalPaymentsFailedNode = doc.CreateElement("TotalPaymentsFailed");
                    totalPaymentsFailedNode.AppendChild(doc.CreateTextNode(res.Data.TotalPaymentsFailed.ToString()));
                    dataNode.AppendChild(totalPaymentsFailedNode);

                    XmlNode totalPaymentsFailedAmountNode = doc.CreateElement("TotalPaymentsFailedAmount");
                    totalPaymentsFailedAmountNode.AppendChild(doc.CreateTextNode(res.Data.TotalPaymentsFailedAmount.ToString()));
                    dataNode.AppendChild(totalPaymentsFailedAmountNode);

                    XmlNode totalPaymentsSuccessfulNode = doc.CreateElement("TotalPaymentsSuccessful");
                    totalPaymentsSuccessfulNode.AppendChild(doc.CreateTextNode(res.Data.TotalPaymentsSuccessful.ToString()));
                    dataNode.AppendChild(totalPaymentsSuccessfulNode);

                    XmlNode totalPaymentsSuccessfulAmountNode = doc.CreateElement("TotalPaymentsSuccessfulAmount");
                    totalPaymentsSuccessfulAmountNode.AppendChild(doc.CreateTextNode(res.Data.TotalPaymentsSuccessfulAmount.ToString()));
                    dataNode.AppendChild(totalPaymentsSuccessfulAmountNode);

                    XmlNode yourGeneralReferenceNode = doc.CreateElement("YourGeneralReference");
                    yourGeneralReferenceNode.AppendChild(doc.CreateTextNode(res.Data.YourGeneralReference));
                    dataNode.AppendChild(yourGeneralReferenceNode);

                    XmlNode YourSystemReferenceNode = doc.CreateElement("YourSystemReference");
                    YourSystemReferenceNode.AppendChild(doc.CreateTextNode(res.Data.YourSystemReference));
                    dataNode.AppendChild(YourSystemReferenceNode);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }

        //GetPaymentDetailPlusNextPaymentInfo
        public static XmlDocument GetPaymentDetailPlusNextPaymentInfo(string DigitalKey, string PaymentReference)
        {
            XmlDocument doc;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfPaymentDetailPlusNextPaymentInfoTHgMB7oL res;
                res = cs.GetPaymentDetailPlusNextPaymentInfo(DigitalKey, PaymentReference);

                doc = new XmlDocument();
                XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(docNode);

                XmlNode dataNode = doc.CreateElement("Data");
                doc.AppendChild(dataNode);

                if (res.Data == null && res.Error > 0)
                {
                    XmlNode errorNode = doc.CreateElement("Error");
                    errorNode.AppendChild(doc.CreateTextNode(res.Error.ToString()));
                    dataNode.AppendChild(errorNode);

                    XmlNode errorMessageNode = doc.CreateElement("ErrorMessage");
                    errorMessageNode.AppendChild(doc.CreateTextNode(res.ErrorMessage));
                    dataNode.AppendChild(errorMessageNode);
                }
                else
                {
                    XmlNode nextPaymentInfoNode = doc.CreateElement("NextPaymentInfo");
                    dataNode.AppendChild(nextPaymentInfoNode);

                    XmlNode customerStatusCodeNode = doc.CreateElement("CustomerStatusCode");
                    customerStatusCodeNode.AppendChild(doc.CreateTextNode(res.Data.NextPaymentInfo.CustomerStatusCode));
                    nextPaymentInfoNode.AppendChild(customerStatusCodeNode);

                    XmlNode customerStatusDescriptionNode = doc.CreateElement("CustomerStatusDescription");
                    customerStatusDescriptionNode.AppendChild(doc.CreateTextNode(res.Data.NextPaymentInfo.CustomerStatusDescription));
                    nextPaymentInfoNode.AppendChild(customerStatusDescriptionNode);

                    XmlNode nextPaymentAmountNode = doc.CreateElement("NextPaymentAmount");
                    nextPaymentAmountNode.AppendChild(doc.CreateTextNode(res.Data.NextPaymentInfo.NextPaymentAmount.ToString()));
                    nextPaymentInfoNode.AppendChild(nextPaymentAmountNode);

                    XmlNode nextPaymentDateNode = doc.CreateElement("NextPaymentDate");
                    nextPaymentDateNode.AppendChild(doc.CreateTextNode(res.Data.NextPaymentInfo.NextPaymentDate.ToString()));
                    nextPaymentInfoNode.AppendChild(nextPaymentDateNode);

                    XmlNode paymentDetailNode = doc.CreateElement("PaymentDetail");
                    dataNode.AppendChild(paymentDetailNode);

                    XmlNode bankFailedReasonNode = doc.CreateElement("BankFailedReason");
                    bankFailedReasonNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.BankFailedReason));
                    paymentDetailNode.AppendChild(bankFailedReasonNode);

                    XmlNode bankReturnCodeNode = doc.CreateElement("BankReturnCode");
                    bankReturnCodeNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.BankReturnCode));
                    paymentDetailNode.AppendChild(bankReturnCodeNode);

                    XmlNode debitDateNode = doc.CreateElement("DebitDate");
                    debitDateNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.DebitDate.ToString()));
                    paymentDetailNode.AppendChild(debitDateNode);

                    XmlNode invoiceIDNode = doc.CreateElement("InvoiceID");
                    invoiceIDNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.InvoiceID));
                    paymentDetailNode.AppendChild(invoiceIDNode);

                    XmlNode paymentAmountNode = doc.CreateElement("PaymentAmount");
                    paymentAmountNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.PaymentAmount.ToString()));
                    paymentDetailNode.AppendChild(paymentAmountNode);

                    XmlNode paymentIDNode = doc.CreateElement("PaymentID");
                    paymentIDNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.PaymentID));
                    paymentDetailNode.AppendChild(paymentIDNode);

                    XmlNode paymentMethodNode = doc.CreateElement("PaymentMethod");
                    paymentMethodNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.PaymentMethod));
                    paymentDetailNode.AppendChild(paymentMethodNode);

                    XmlNode paymentReferenceNode = doc.CreateElement("PaymentReference");
                    paymentReferenceNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.PaymentReference));
                    paymentDetailNode.AppendChild(paymentReferenceNode);

                    XmlNode paymentStatusNode = doc.CreateElement("PaymentStatus");
                    paymentStatusNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.PaymentStatus));
                    paymentDetailNode.AppendChild(paymentStatusNode);

                    XmlNode settlementDateNode = doc.CreateElement("SettlementDate");
                    settlementDateNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.SettlementDate.ToString()));
                    paymentDetailNode.AppendChild(settlementDateNode);

                    XmlNode scheduledAmountNode = doc.CreateElement("ScheduledAmount");
                    scheduledAmountNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.ScheduledAmount.ToString()));
                    paymentDetailNode.AppendChild(scheduledAmountNode);

                    XmlNode transactionFeeClientNode = doc.CreateElement("TransactionFeeClient");
                    transactionFeeClientNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.TransactionFeeClient.ToString()));
                    paymentDetailNode.AppendChild(transactionFeeClientNode);

                    XmlNode transactionFeeCustomerNode = doc.CreateElement("TransactionFeeCustomer");
                    transactionFeeCustomerNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.TransactionFeeCustomer.ToString()));
                    paymentDetailNode.AppendChild(transactionFeeCustomerNode);

                    XmlNode yourGeneralReferenceNode = doc.CreateElement("YourGeneralReference");
                    yourGeneralReferenceNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.YourGeneralReference));
                    paymentDetailNode.AppendChild(yourGeneralReferenceNode);

                    XmlNode YourSystemReferenceNode = doc.CreateElement("YourSystemReference");
                    YourSystemReferenceNode.AppendChild(doc.CreateTextNode(res.Data.PaymentDetail.YourSystemReference));
                    paymentDetailNode.AppendChild(YourSystemReferenceNode);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return doc;
        }

        //Get Payment Status
        public static string GetPaymentStatus(string DigitalKey, string PaymentReference)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.GetPaymentStatus(DigitalKey, PaymentReference);

                if (rs.Data == "W")
                    message = "Payment is scheduled waiting to be sent to the bank";
                else if (rs.Data == "P")
                    message = "Payment request has been sent to the bank for processing and Ezidebit is waiting on a success or fail response before completing settlement";
                else if (rs.Data == "S")
                    message = "Payment has been successfully debited from the Customer and deposited to the client's settlement bank account";
                else if (rs.Data == "D")
                    message = "Payment has been dishonoured by the Customer's financial institution due to insufficient funds";
                else if (rs.Data == "F")
                    message = "Payment has been dishonoured by the Customer's financial institution";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Clear Schedule to EziDebit
        public static string ClearSchedule(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, string KeepManualPayments, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.ClearSchedule(DigitalKey, EziDebitCustomerID, YourSystemReference, KeepManualPayments, Username);

                if (rs.Data == "S")
                    message = "Schedule Clear Successfully";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Delete Payment to EziDebit
        public static string DeletePayment(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, string PaymentReference, string DebitDate, long PaymentAmountInCents, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.DeletePayment(DigitalKey, EziDebitCustomerID, YourSystemReference, PaymentReference, DebitDate, PaymentAmountInCents, Username);

                if (rs.Data == "S")
                    message = "Delete Payment Successfully";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //IsBsbValid
        public static string IsBsbValid(string DigitalKey, string BankAccountBSB)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.IsBsbValid(DigitalKey, BankAccountBSB);

                if (rs.Data == "YES")
                    message = "BSB number provided is valid and can be accepted in the Ezidebit systems";
                else if (rs.Data == "NO")
                    message = "BSB number is invalid, and if provided will be rejected by the Ezidebit systems";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //IsBsbValid
        public static string IsSystemLocked(string DigitalKey)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.IsSystemLocked(DigitalKey);

                if (rs.Data == "YES")
                    message = "The core databases has been restricted and any updates must be submitted at a later time";
                else if (rs.Data == "NO")
                    message = "The core databases exists and updates will be processed as and when the methods are invoked";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //PaymentExchangeVersion
        public static string PaymentExchangeVersion()
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.PaymentExchangeVersion();

                if (rs.Error > 0)
                    message = rs.ErrorMessage;
                else
                    message = "Payment Exchange Version Is:" + rs.Data;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }

        //Change Customer Status 
        public static string ChangeCustomerStatus(string DigitalKey, string EziDebitCustomerID, string YourSystemReference, string NewStatus, string Username)
        {
            string message = string.Empty;
            try
            {
                EziDebitService.NonPCIServiceClient cs = new EziDebitService.NonPCIServiceClient("BasicHttpBinding_INonPCIService1");
                EziDebitService.EziResponseOfstring rs;
                rs = cs.ChangeCustomerStatus(DigitalKey, EziDebitCustomerID, YourSystemReference, NewStatus, Username);

                if (rs.Data == "S")
                    message = "Customer status was updated successfully";
                else
                    message = rs.ErrorMessage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return message;
        }
    }
}
